# ANSI Colors for pretty output
ANSI_COLOR_RED    =\033[0;31m
ANSI_COLOR_GREEN  =\033[0;32m
ANSI_COLOR_YELLOW =\033[0;33m
ANSI_COLOR_BLUE   =\033[0;34m
ANSI_COLOR_MAGENTA=\033[0;35m
ANSI_COLOR_CYAN   =\033[0;36m
ANSI_COLOR_RESET  =\033[0;0m
ANSI_BOLD         =\033[0;1m

# Module name
MOD = RecurCarrySaveAdder

# Set SRC_DIR if not defined
SRC_DIR ?= ../../src

# Directory structure
RTL_DIR = $(SRC_DIR)
ARTIFACT_DIR = $(MOD)Artifact

# Target executable
TARGET = $(ARTIFACT_DIR)/V$(MOD)

# Default target
all: $(TARGET)

$(MOD).waveform.vcd: $(TARGET)
	@echo
	@echo "$(ANSI_COLOR_BLUE)$(ANSI_BOLD)======= SIMULATING =======$(ANSI_COLOR_RESET)"
	./$(TARGET)

$(TARGET): $(RTL_DIR)/$(MOD).sv $(MOD).cpp
	@echo
	@echo "$(ANSI_COLOR_BLUE)$(ANSI_BOLD)======== COMPILING ========$(ANSI_COLOR_RESET)"
	@verilator -Wall --trace --sv --timing \
		-Mdir $(ARTIFACT_DIR) \
		-cc $(RTL_DIR)/$(MOD).sv \
		--top $(MOD) \
		--exe $(MOD).cpp \
		-I$(RTL_DIR)
	@make -C $(ARTIFACT_DIR) -f V$(MOD).mk V$(MOD)
	@echo "$(ANSI_COLOR_GREEN)Build complete: $(TARGET)$(ANSI_COLOR_RESET)"

run: $(MOD).waveform.vcd
	@echo "$(ANSI_COLOR_GREEN)Simulation complete$(ANSI_COLOR_RESET)"

clean:
	@echo "$(ANSI_COLOR_YELLOW)Cleaning $(MOD) testbench$(ANSI_COLOR_RESET)"
	@rm -rf $(ARTIFACT_DIR)
	@rm -f $(MOD).waveform.vcd

.PHONY: all clean run 